package com.yyfax.pay.common.constants;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.yyfax.commons.lang.DateUtil;

/**
 * 定时器常量配置
 * 
 * @author buyi
 * @date 2018-01-24 14:01:07
 * @since v2.0.8
 */
@Component
public class QuartzConstant {

	/** 默认下次查询执行时间间隔，单位分,1天 */
	private static final int DEFAULT_QUERY_NEXT_TIME_INTERVAL = 1440;

	/** 默认下次执行时间间隔，单位秒 ,60分钟 */
	private static final int DEFAULT_NOTIFY_NEXT_TIME_INTERVAL = 3600;

	/**
	 * 线程池最大线程数
	 */
	@Value("${quartz.pool.maxSize:10}")
	private int quartzPoolMaxSize;

	/**
	 * 线程池最大存活期时间
	 */
	@Value("${quartz.pool.keepAlive:1}")
	private int quartzPoolKeepAlive;

	/**
	 * 线程池最大queue长度
	 */
	@Value("${quartz.pool.queueSize:10000}")
	private int quartzPoolQueueSize;

	/**
	 * 通知连接超时
	 */
	@Value("${quartz.notify.connectTimeout:10000}")
	private int notifyConnectTimeout;

	/**
	 * 通知读取超时
	 */
	@Value("${quartz.notify.readTimeout:10000}")
	private int notifyReadTimeout;

	/**
	 * 异步通知梯度时间间隔，单位秒
	 */
	@Value("${quartz.notify.interval:60,300,1800,3600}")
	private String notifyNextTimeInterval;

	/**
	 * 查询梯度时间间隔，单位分钟
	 */
	@Value("${quartz.query.interval:30,40,60,1440}")
	private String queryNextTimeInterval;

	/** 分页查询，默认每页数据量 */
	@Value("${quartz.task.query_page_size:1000}")
	private int queryPageSize;

	public int getQuartzPoolMaxSize() {
		return quartzPoolMaxSize;
	}

	public void setQuartzPoolMaxSize(int quartzPoolMaxSize) {
		this.quartzPoolMaxSize = quartzPoolMaxSize;
	}

	public int getQuartzPoolKeepAlive() {
		return quartzPoolKeepAlive;
	}

	public void setQuartzPoolKeepAlive(int quartzPoolKeepAlive) {
		this.quartzPoolKeepAlive = quartzPoolKeepAlive;
	}

	public int getQuartzPoolQueueSize() {
		return quartzPoolQueueSize;
	}

	public void setQuartzPoolQueueSize(int quartzPoolQueueSize) {
		this.quartzPoolQueueSize = quartzPoolQueueSize;
	}

	public int getNotifyConnectTimeout() {
		return notifyConnectTimeout;
	}

	public void setNotifyConnectTimeout(int notifyConnectTimeout) {
		this.notifyConnectTimeout = notifyConnectTimeout;
	}

	public int getNotifyReadTimeout() {
		return notifyReadTimeout;
	}

	public void setNotifyReadTimeout(int notifyReadTimeout) {
		this.notifyReadTimeout = notifyReadTimeout;
	}

	public String getNotifyNextTimeInterval() {
		return notifyNextTimeInterval;
	}

	public void setNotifyNextTimeInterval(String notifyNextTimeInterval) {
		this.notifyNextTimeInterval = notifyNextTimeInterval;
	}

	public String getQueryNextTimeInterval() {
		return queryNextTimeInterval;
	}

	public void setQueryNextTimeInterval(String queryNextTimeInterval) {
		this.queryNextTimeInterval = queryNextTimeInterval;
	}

	public int getQueryPageSize() {
		return queryPageSize;
	}

	public void setQueryPageSize(int queryPageSize) {
		this.queryPageSize = queryPageSize;
	}

	/**
	 * 获取下一次执行的时间间隔，从0开始
	 * 
	 * @author buyi
	 * @date 2017-09-04 14:04:11
	 * @since v2.0.0
	 * @param index
	 * @return
	 */
	public int getNotifyNextTimeInterval(int index) {
		if (StringUtils.isBlank(this.notifyNextTimeInterval)) {
			throw new IllegalArgumentException("任务执行间缺少有配置task.config.next_time_interval");
		}

		String[] nextTimeIntervals = this.notifyNextTimeInterval.split(",");
		if (nextTimeIntervals.length - 1 < index) {
			return DEFAULT_NOTIFY_NEXT_TIME_INTERVAL;
		}

		return Integer.valueOf(nextTimeIntervals[index]);
	}

	/**
	 * 获取下一次执行时间，从0开始
	 * 
	 * @author buyi
	 * @date 2017-09-04 14:08:02
	 * @since v2.0.0
	 * @param index
	 * @return
	 */
	public String getNotifyNextTime(int index) {

		int second = getNotifyNextTimeInterval(index);

		return DateUtil.datetime2Str(DateUtil.addSecond(new Date(), second));

	}

	/**
	 * 获取下一次查询执行的时间间隔，从0开始
	 * 
	 * @author buyi
	 * @date 2018-01-10 16:51:19
	 * @since v2.0.4
	 * @param index
	 * @return
	 */
	public int getQueryNextTimeInterval(int index) {
		if (StringUtils.isBlank(this.queryNextTimeInterval)) {
			throw new IllegalArgumentException("任务执行间隔缺少配置task.config.query_next_time_interval");
		}

		String[] nextTimeIntervals = this.queryNextTimeInterval.split(",");
		if (nextTimeIntervals.length - 1 < index) {
			return DEFAULT_QUERY_NEXT_TIME_INTERVAL;
		}

		return Integer.valueOf(nextTimeIntervals[index]);
	}

	/**
	 * 获取下次执行时间，index起始位置从0开始
	 * 
	 * @author buyi
	 * @date 2018-01-10 16:52:06
	 * @since v2.0.4
	 * @param index
	 * @return
	 */
	public String getQueryNextTime(int index) {

		int minute = getQueryNextTimeInterval(index);

		return DateUtil.datetime2Str(DateUtil.addMinute(new Date(), minute));
	}
}
